package org.codetranslators.common;

import java.util.HashMap;
import java.util.Vector;

public class DominatorTree {
	private HashMap<Node, DominatorTreeNode> nodeVsDominatorTreeNode;
	private DominatorTreeNode dominatorTreeRoot;

	public void setGraphNodeVsDominatorTreeNode(HashMap<Node, DominatorTreeNode> nodeVsDominatorTreeNode){
		this.nodeVsDominatorTreeNode = nodeVsDominatorTreeNode;
	}

	public Node getParentInTree(Node graphNode){
		DominatorTreeNode dtNode = (DominatorTreeNode)nodeVsDominatorTreeNode.get(graphNode);
		DominatorTreeNode parentDTNode = (DominatorTreeNode)dtNode.getParent();
		return parentDTNode.getGraphNode();
	}

	public Vector<Node> getChildNodesForGraphNode(Node graphNode){
		Vector<Node> children = new Vector<Node>();
		DominatorTreeNode dtNode = (DominatorTreeNode)nodeVsDominatorTreeNode.get(graphNode);
		Vector<TreeNode> childrenInDomTree = dtNode.getChildren();
		if(childrenInDomTree == null) return children;   // Has no children in dominator tree

		for(int i = 0; i < childrenInDomTree.size(); i++){
			DominatorTreeNode childDTNode = (DominatorTreeNode) childrenInDomTree.elementAt(i);
			children.addElement(childDTNode.getGraphNode());
		}

		return children;
	}

	public void setDominatorTreeRoot(DominatorTreeNode dominatorTreeRoot) {
		this.dominatorTreeRoot = dominatorTreeRoot;
	}

	public DominatorTreeNode getDominatorTreeRoot(){
		return dominatorTreeRoot;
	}

	public DominatorTreeNode getDominatorTreeNode(Node graphNode){
		return (DominatorTreeNode)nodeVsDominatorTreeNode.get(graphNode);
	}

	public HashMap<Node, DominatorTreeNode> getNodeVsDominatorTreeNode() {
		return nodeVsDominatorTreeNode;
	}

}
